<?php
/**
 * Created by PhpStorm.
 * User: paperen
 * Date: 2020/6/17
 * Time: 21:09
 */

namespace App\Repositories\Task;

use App\Repositories\Repository;
use Illuminate\Support\Facades\DB;


/**
 * Class TaskRepository
 * @package App\Repositories\Task
 * @version v1.3
 */
class TaskRepository extends Repository
{
	// 优质任务多少天过期
	protected $_diff = 3;

	const STATE_APPROVALED = 2;
	const STATE_OVER = 4;

    /**
     * @return string
     */
    public function model()
    {
        return \App\Models\Task\TaskModel::class;
    }

    public function getOverTask() {
    	$date = date('Y-m-d H:i:s');
		return $this->model
			->where('finishtime', '<=', $date)
			->where('state', '<=', self::STATE_APPROVALED)
			->pluck('id');
	}

	public function getOverHighTask() {
		$date = date('Y-m-d H:i:s', strtotime("-{$this->_diff} days"));
		return $this->model
			->where('approvaledtime', '<', $date)
			->where('is_high', 1)
			->pluck('id');
	}

	public function setTaskHigh($id, $high=1) {
		return $this->model
			->where('id', $id)
			->update([
				'is_high' => $high
			]);
	}

	public function getNearlyOverTask($daysDiff=1) {
    	if ( $daysDiff <= 0 ) $daysDiff = 1;
		$date1 = date('Y-m-d H:i:00', strtotime("+{$daysDiff} days"));
		$date2 = date('Y-m-d H:i:59', strtotime("+{$daysDiff} days"));
		return $this->model
			->where('finishtime', '>=', $date1)
			->where('finishtime', '<=', $date2)
			->where('state', self::STATE_APPROVALED)
			->pluck('id');
	}

	public function incQuotaRemain($taskId, $num=1) {
		return $this->model
			->where('id', $taskId)
			->increment('quota_remain', $num);
	}

	public function getById($taskId) {
		return $this->model
			->where('id', $taskId)
			->first();
	}

	/**
	 * 获取触发积分返还的任务
	 */
	public function getRebackIntegralTask() {
		$date1 = date('Y-m-d H:i:00');
		$date2 = date('Y-m-d H:i:59');
		return $this->model
			->where('rebacktime', '>=', $date1)
			->where('rebacktime', '<=', $date2)
			->where('state', self::STATE_OVER)
			->pluck('id');
	}
}
